home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SATAN11.ZIP / SRC / PORT_SCA / ERROR.C next >
Encoding:
C/C++ Source or Header  |  1995-04-04  |  2.3 KB  |  112 lines

  1.  /*
  2.   * remark, error, panic - diagnostics handlers
  3.   * 
  4.   * Feature: %m is expanded to the system errno text.
  5.   * 
  6.   * Author: Wietse Venema.
  7.   */
  8.  
  9. #include <stdio.h>
  10. #include <errno.h>
  11.  
  12. #ifdef __STDC__
  13. #include <stdarg.h>
  14. #define VARARGS(func,type,arg) func(type arg, ...)
  15. #define VASTART(ap,type,name)  va_start(ap,name)
  16. #define VAEND(ap)              va_end(ap)
  17. #else
  18. #include <varargs.h>
  19. #define VARARGS(func,type,arg) func(va_alist) va_dcl
  20. #define VASTART(ap,type,name)  {type name; va_start(ap); name = va_arg(ap, type)
  21. #define VAEND(ap)              va_end(ap);}
  22. #endif
  23.  
  24. char   *progname = "unknown";
  25.  
  26. extern int errno;
  27. extern char *strerror();
  28. extern char *strcpy();
  29.  
  30. #include "lib.h"
  31.  
  32. /* percentm - replace %m by error message associated with value in err */
  33.  
  34. char   *percentm(buf, str, err)
  35. char   *buf;
  36. char   *str;
  37. int     err;
  38. {
  39.     char   *ip = str;
  40.     char   *op = buf;
  41.  
  42.     while (*ip) {
  43.     switch (*ip) {
  44.     case '%':
  45.         switch (ip[1]) {
  46.         case '\0':                /* don't fall off end */
  47.         *op++ = *ip++;
  48.         break;
  49.         case 'm':                /* replace %m */
  50.         strcpy(op, strerror(err));
  51.         op += strlen(op);
  52.         ip += 2;
  53.         break;
  54.         default:                /* leave %<any> alone */
  55.         *op++ = *ip++, *op++ = *ip++;
  56.         break;
  57.         }
  58.     default:
  59.         *op++ = *ip++;
  60.     }
  61.     }
  62.     *op = 0;
  63.     return (buf);
  64. }
  65.  
  66. /* error - print warning on stderr and terminate */
  67.  
  68. void    VARARGS(error, char *, fmt)
  69. {
  70.     va_list ap;
  71.     int     err = errno;
  72.     char    buf[BUFSIZ];
  73.  
  74.     VASTART(ap, char *, fmt);
  75.     fprintf(stderr, "%s: ", progname);
  76.     vfprintf(stderr, percentm(buf, fmt, err), ap);
  77.     fprintf(stderr, "\n");
  78.     VAEND(ap);
  79.     exit(1);
  80. }
  81.  
  82. /* remark - print warning on stderr and continue */
  83.  
  84. void    VARARGS(remark, char *, fmt)
  85. {
  86.     va_list ap;
  87.     int     err = errno;
  88.     char    buf[BUFSIZ];
  89.  
  90.     VASTART(ap, char *, fmt);
  91.     fprintf(stderr, "%s: ", progname);
  92.     vfprintf(stderr, percentm(buf, fmt, err), ap);
  93.     fprintf(stderr, "\n");
  94.     VAEND(ap);
  95. }
  96.  
  97. /* panic - print warning on stderr and dump core */
  98.  
  99. void    VARARGS(panic, char *, fmt)
  100. {
  101.     va_list ap;
  102.     int     err = errno;
  103.     char    buf[BUFSIZ];
  104.  
  105.     VASTART(ap, char *, fmt);
  106.     fprintf(stderr, "%s: ", progname);
  107.     vfprintf(stderr, percentm(buf, fmt, err), ap);
  108.     fprintf(stderr, "\n");
  109.     VAEND(ap);
  110.     abort();
  111. }
  112.